package cn.bocweb.spinning.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.common.BaseActivity;
import cn.bocweb.spinning.view.dialog.LoadingDialog;

public class IntegralProductActivity extends BaseActivity {

    int myScore;
    int productScore;
    String id;
    LoadingDialog dialog;

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.website)
    WebView website;
    @Bind(R.id.button_recharge)
    Button button_recharge;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_integral_product);
        ButterKnife.bind(this);

        dialog = new LoadingDialog(this);
        dialog.show();

        initToolbar();
        initIntent();
    }

    private void initIntent() {
        Intent intent = getIntent();

        //0为隐藏, 1为显示
        id = intent.getStringExtra(Constant.TO_SHOP_DETAIL_ID);

        int status = intent.getIntExtra(Constant.TO_SHOP_DETAIL_STATUS, 0);
        if (status == 1) {
            button_recharge.setVisibility(View.GONE);
        }

        String url = intent.getStringExtra(Constant.TO_SHOP_DETAIL_URL);
        if (TextUtils.isEmpty(url)) {
            DeviceUtil.MyToast(this, "无法获得商品详情");
            return;
        }

        myScore = intent.getIntExtra(Constant.TO_SHOP_DETAIL_MY_INTEGRAL, 0);

        productScore = intent.getIntExtra(Constant.TO_SHOP_DETAIL_INTEGRAL, 0);

        initWebsite(url);

    }

    private void initWebsite(String url) {
        website.loadUrl(url);
        website.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                dialog.dismiss();
            }
        });
    }

    private void initToolbar() {
        toolbar.setTitle("兑换商城");
        toolbar.setTitleTextColor(getResources().getColor(R.color.white));
        toolbar.setNavigationIcon(R.mipmap.btn_exit);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @OnClick(R.id.button_recharge)
    public void onClick() {
        if (productScore <= myScore) {
            Intent intent = new Intent(this, IntegralAddrActivity.class);
            intent.putExtra(Constant.TO_SHOP_ADDR_ID, id);
            startActivity(intent);
        } else {
            DeviceUtil.MyToast(this, "积分不足");
        }
    }
}
